//
//  WelcomeViewController.swift
//  weibo
//
//  Created by Fosen on 15/12/1.
//  Copyright © 2015年 fosen. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class WelcomeViewController: UIViewController {

    
    //loadView阶段  替换根视图
    override func loadView() {
        view = backView
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()

        iconView.sd_setImageWithURL(UserAccountViewModel().welcomeImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
        // Do any additional setup after loading the view.
    }
    
    //动画效果建议在viewDidAppear 中实现
    override func viewDidAppear(animated: Bool) {
        startAnimation()
    }
    
    private func startAnimation(){
    
        //提前更改约束
        iconView.snp_updateConstraints(closure: { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(-520)
            
        })
        
       UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 6.8, options: [], animations: { () -> Void in
        
        //强制提前刷新界面
        self.view.layoutIfNeeded()
        
        }) { (_) -> Void in
//            print("ok")
            
            NSNotificationCenter.defaultCenter().postNotificationName(WebSwitchRootVCNotification, object: nil)

        }
    
    }
    
    

    
    private func setupUI(){
        
        //添加子视图
        
        view.addSubview(iconView)
        
        view.addSubview(welcomeLabel)
        
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(30)
        }
        
        iconView.layer.cornerRadius = 50
        iconView.layer.masksToBounds = true
    }
    

    private lazy var backView : UIImageView = UIImageView(image: UIImage(named: "ad_background"))

    private lazy var iconView : UIImageView = UIImageView ()
    private lazy var welcomeLabel : UILabel = UILabel(title: "欢迎回来  " + (UserAccountViewModel().userName ?? ""), color: UIColor.darkGrayColor(), fontSize: 18)

    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
